package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.OrderBean;
import com.project.bean.ShopBean;
import com.project.mapper.IOrderInfoMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShopMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IShopMapper shopMapper;
    @Autowired
    private IOrderInfoMapper infoMapper;
    @Override
    public IPage<OrderBean> cutOrdersById(Integer pageNum, Integer userId) {
        return null;
    }

    @Override
    public OrderBean findById(Integer id) {
        return null;
    }

    @Override
    public void addOrder(OrderBean order) {
        //查询用户购物车集合
        List<ShopBean> shopList = shopMapper.findByUser(order.getUserId());
        //设置订单总价
        order.setMoney(this.getTotalPrice(shopList));
        //添加订单
        orderMapper.insert(order);
        //添加订单明细
        infoMapper.addInfoList(order.getId(),shopList);
        //清空购物车
        QueryWrapper<ShopBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",order.getUserId());

        shopMapper.delete(qw);
    }
    private Integer getTotalPrice(List<ShopBean> shopList){
        int total = 0;
        for(ShopBean shop : shopList){
            total += shop.getNum() * shop.getProduct().getPrice();
        }
        return total;
    }

    @Override
    public List<OrderBean> findByStatus() {
        return null;
    }

    @Override
    public void updateStatusToFinish(Integer id) {

    }

    @Override
    public IPage<OrderBean> cutByItem(Integer pageNum, String status, LocalDate startDate, LocalDate endDate) {
        return null;
    }

    @Override
    public void updateStatusToSend(Integer id) {

    }
}
